const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.connect('mongodb://localhost:27017/xqmanager', { useNewUrlParser: true, useUnifiedTopology: true });
const UserSchema = new Schema({
  username: { type: String, default: '' },//有默认值的属性再插入时不添加会自动以默认值插入
  age: { type: Number },
  telnumber: { type: Number },
  userid: { type: Number },
  remarks: { contents: { type: String }, date: { type: Date, default: Date.now } },//对插入这个数组的每一项对象数据的约束条件
});
const User = mongoose.model('user', UserSchema);

exports.adduser = function (user, callback) {
  User.find({
    username: user.name
  }, function (err, res) {
    if (err) {
    }
    else {
      if (res.length == 0) {
        const userinfo = new User({
          username: user.name,
          age: user.age,
          telnumber: user.telnumber,
          userid: user.userid,
          remarks: { contents: user.contents },
        })

        userinfo.save().then(() => {
          callback('500')
        });
      }
      else {
        console.log(res)
        callback('404')
      }
    }
  })

}


exports.searchall = function (page, limit, callback) {
  User.find(function (err, res) {
    if (err) {
      console.log(err)
    }
    else {

      let data = res.slice((page - 1) * limit, limit * page)
      console.log(data)
      callback(res, data)
    }
  })

}


exports.search = function (type, info, page, limit, callback) {
  if (type === 'id') {
    User.find({
      _id: info
    }, function (err, res) {
      if (err) {
        console.log(err)
      }
      else {
        let data = res.slice((page - 1) * limit, limit * page)
        console.log(data)
        callback(res, data)

      }
    })
  }
  if (type === 'username') {
    User.find({
      username: info
    }, function (err, res) {
      if (err) {
        console.log(err)
      }
      else {

        let data = res.slice((page - 1) * limit, limit * page)
        console.log(data)
        callback(res, data)
      }
    })
  }
  if (type === 'age') {
    User.find({
      age: info
    }, function (err, res) {
      if (err) {
        console.log(err)
      }
      else {

        let data = res.slice((page - 1) * limit, limit * page)
        console.log(data)
        callback(res, data)
      }
    })
  }
  if (type === 'telnumber') {
    User.find({
      telnumber: info
    }, function (err, res) {
      if (err) {
        console.log(err)
      }
      else {

        let data = res.slice((page - 1) * limit, limit * page)
        console.log(data)
        callback(res, data)
      }
    })
  }
  if (type === 'userid') {
    User.find({
      userid: info
    }, function (err, res) {
      if (err) {
        console.log(err)
      }
      else {

        let data = res.slice((page - 1) * limit, limit * page)
        console.log(data)
        callback(res, data)
      }
    })
  }
  if (type === 'contents') {
    User.find({
      remarks: { contents: info }
    }, function (err, res) {
      if (err) {
        console.log(err)
      }
      else {

        let data = res.slice((page - 1) * limit, limit * page)
        console.log(data)
        callback(res, data)
      }
    })
  }


}

exports.remove = function (username, callback) {
  User.remove({
    username
  }, function (err, ret) {
    if (err) {
      console.log('删除失败')
    }
    else {
      callback(ret)
    }
  })

}

exports.updateuser = function (id, username, age, telnumber, userid, contents, callback) {
  User.findByIdAndUpdate(id, {
    username,
    age,
    telnumber,
    userid,
    remarks: { contents }
  }, function (err, ret) {
    if (err) {
      console.log('更新失败')
    } else {
      callback(ret)
    }
  })



}